package com.iotteam.androidbase.moudules.sensor_setting.presenter;

import android.text.TextUtils;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.BaseActivity;
import com.iotteam.androidbase.base.BaseApplication;
import com.iotteam.androidbase.commen.LocalInfoManager;
import com.iotteam.androidbase.commen.network.Api;
import com.iotteam.androidbase.commen.network.core.HttpListener;
import com.iotteam.androidbase.commen.network.core.HttpManager;
import com.iotteam.androidbase.commen.utils.PLog;
import com.iotteam.androidbase.commen.utils.ToastUtil;
import com.iotteam.androidbase.moudules.sensor_history.model.HistoryModel;
import com.iotteam.androidbase.moudules.sensor_setting.model.SensorSettingRetModel;
import com.iotteam.androidbase.moudules.sensor_setting.view.ISensorSettingView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lidechen on 6/25/17.
 */

public class SensorSettingPresenter implements ISensorSettingPresenter {

    private static final String TAG = "SensorSettingPresenter";

    private ISensorSettingView mView;

    public SensorSettingPresenter(ISensorSettingView view){
        mView = view;
    }

    @Override
    public void sendMessageToSensor() {

        Map<String, String> param = new HashMap<>();
        param.put("mobile", LocalInfoManager.getInstance().getUserInfo().getMobile());

        param.put("terminalid", "861694034305647");
        param.put("password", "bb0a39");
        param.put("message1", mView.getMessage1());
        param.put("message2", mView.getMessage2());
        param.put("message3", mView.getMessage3());

        mView.setLoadingString(BaseApplication.getAppContext().getString(R.string.dialog_sensor_altering));

        HttpManager.getInstance().postBean(Api.alterTer, param, SensorSettingRetModel.class, new HttpListener<SensorSettingRetModel>() {
            @Override
            public void onSucceed(int what, SensorSettingRetModel bean) {
                if(bean.getCode() == 0){

                    ToastUtil.showShort(R.string.label_activity_sensor_setting_toast_alter_success);
                    PLog.i(TAG, bean.toString());
                }else{
                    if(!TextUtils.isEmpty(bean.getMessage())) {
                        ToastUtil.showShort(bean.getMessage());
                    }else{
                        ToastUtil.showShort(R.string.dialog_reuest_info_err);
                    }
                }
            }

            @Override
            public void onFailed(int what, Exception e) {
                ToastUtil.showShort(R.string.label_activity_sensor_setting_toast_alter_faile);
            }

            @Override
            public void onStart(int what) {
                mView.showLoading();
            }

            @Override
            public void onFinish(int what) {
                mView.dismissLoading();
            }
        });
    }
}
